;(function (win, lib) {
  const doc = win.document
  const docEl = doc.documentElement
  const devicePixelRatio = win.devicePixelRatio
  let dpr = 1
  let scale = 1
  function setViewport() {
    dpr = 1
    win.devicePixelRatioValue = dpr
    scale = 1 / dpr
    const metaEl = doc.createElement('meta')
    metaEl.setAttribute('name', 'viewport')
    metaEl.setAttribute(
      'content',
      'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no'
    )
    if (docEl.firstElementChild) {
      docEl.firstElementChild.appendChild(metaEl)
    } else {
      const wrap = doc.createElement('div')
      wrap.appendChild(metaEl)
      doc.write(wrap.innerHTML)
    }
  }
  setViewport()
  let newBase = 100
  function setRem() {
    const clientWidth = docEl.getBoundingClientRect().width
    const visualView = Math.min(docEl.getBoundingClientRect().width, lib.maxWidth) // visual viewport
    newBase = (100 * visualView) / lib.desinWidth
    docEl.style.fontSize = newBase + 'px'
    if (clientWidth > 1080 && clientWidth < 1240) {
      docEl.style.fontSize = 86 + 'px'
    } else if (clientWidth > 1920) {
      docEl.style.fontSize = 133 + 'px'
    }
  }
  let tid = ''
  lib.desinWidth = 750
  lib.maxWidth = 750
  lib.init = function () {
    win.addEventListener(
      'resize',
      function () {
        clearTimeout(tid)
        tid = setTimeout(setRem, 300)
      },
      false
    )
    win.addEventListener(
      'pageshow',
      function (e) {
        if (e.persisted) {
          clearTimeout(tid)
          tid = setTimeout(setRem, 300)
        }
      },
      false
    )
    setRem()
    docEl.setAttribute('data-dpr', dpr)
  }
})(window, window.adaptive || (window.adaptive = {}))

window.adaptive.desinWidth = 750
window.adaptive.maxWidth = 750
window.adaptive.init()
